<%@ include file="/include/taglib.jsp"%>

<c:choose>
	<c:when test="${itemField.class.name == 'org.computate.model.item.field.ItemField' and (not empty user or itemField.approved)}">
		<c:choose>
			<c:when test="${itemField.c.name == 'java.lang.Boolean' or itemField.c.name == 'boolean'}">
				<div data-role="fieldcontain">
					<fieldset data-role="controlgroup" data-type="horizontal" >
						<legend>${fn:escapeXml(itemField.label)}</legend>
						<c:choose>
							<c:when test="${empty user}">
								<div>${item[itemField.name] ? 'Yes' : 'No'}</div>
							</c:when>
							<c:otherwise>
								<c:choose>
									<c:when test="${item[itemField.name]}">
										<input name="${fn:escapeXml(itemField.name)}" type="radio" id="${fn:escapeXml(itemField.name)}_yes" value="true" checked="checked" onchange="$('#${fn:escapeXml(itemField.name)}_collapsible').trigger('expand'); $.ajax({url: '/json/item/edit.json?model=' + escape('${item.modelName}') + '&amp;id=${item.id}&amp;field=' + escape('${itemField.name}') + '&amp;update_value=true'});"/>
										<label for="${fn:escapeXml(itemField.name)}_yes">Yes</label>
										<input name="${fn:escapeXml(itemField.name)}" type="radio" id="${fn:escapeXml(itemField.name)}_no" value="false" onchange="$('#${fn:escapeXml(itemField.name)}_collapsible').trigger('collapse'); $.ajax({url: '/json/item/edit.json?model=' + escape('${item.modelName}') + '&amp;id=${item.id}&amp;field=' + escape('${itemField.name}') + '&amp;update_value=false'});"/>
										<label for="${fn:escapeXml(itemField.name)}_no">No</label>
									</c:when>
									<c:otherwise>
										<input name="${fn:escapeXml(itemField.name)}" type="radio" id="${fn:escapeXml(itemField.name)}_yes" value="true" onchange="$('#${fn:escapeXml(itemField.name)}_collapsible').trigger('expand'); $.ajax({url: '/json/item/edit.json?model=' + escape('${item.modelName}') + '&amp;id=${item.id}&amp;field=' + escape('${itemField.name}') + '&amp;update_value=true'});"/>
										<label for="${fn:escapeXml(itemField.name)}_yes">Yes</label>
										<input name="${fn:escapeXml(itemField.name)}" type="radio" id="${fn:escapeXml(itemField.name)}_no" value="false" checked="checked" onchange="$('#${fn:escapeXml(itemField.name)}_collapsible').trigger('collapse'); $.ajax({url: '/json/item/edit.json?model=' + escape('${item.modelName}') + '&amp;id=${item.id}&amp;field=' + escape('${itemField.name}') + '&amp;update_value=false'});"/>
										<label for="${fn:escapeXml(itemField.name)}_no">No</label>
									</c:otherwise>
								</c:choose>
							</c:otherwise>
						</c:choose>
					</fieldset>
				</div>
			</c:when>
			<c:when test="${itemField.c.name == 'java.util.Date'}">
				<div data-role="fieldcontain">
				    <fieldset data-role="controlgroup">
						<legend>${fn:escapeXml(itemField.label)}</legend>
						<input name="${fn:escapeXml(itemField.name)}" type="text" class="input_dob ${itemClass}"/>
					</fieldset>
				</div>
			</c:when>
			<c:when test="${itemField.c.name == 'java.util.List' and not empty itemField.itemName}">
				<c:if test="${not empty user}">
					<div data-role="fieldcontain">
						<fieldset data-role="controlgroup">
							<form action="/page/item/${fn:escapeXml(itemField.itemName)}/${fn:escapeXml(itemField.itemName)}Add.jsp" data-ajax="false" id="form_${fn:escapeXml(itemField.itemName)}Add" name="form_${fn:escapeXml(itemField.itemName)}Add" class="form_validate" method="post">
								<legend></legend>
						
								<div>
									<a href="/page/item/${fn:escapeXml(itemField.itemName)}/${fn:escapeXml(itemField.itemName)}Index.jsp">Edit ${fn:escapeXml(itemField.itemPluralName)}</a>
								</div>
								<input type="hidden" name="parentField" value="${fn:escapeXml(itemField.parentField)}"/>
								<input type="hidden" name="parentId" value="${item.id}"/>
								<input data-inline="true" type="text" name="name" id="${fn:escapeXml(itemField.itemName)}_name" placeholder="${fn:escapeXml(itemField.itemDisplayName)} Name" />
								<button data-inline="true" type="submit" data-icon="plus" name="submit" value="${fn:escapeXml(itemField.itemName)}">Add ${fn:escapeXml(itemField.itemDisplayName)}</button>
							</form>
						</fieldset>
					</div>
				</c:if>
				
				<c:choose>
					<c:when test="${not empty user}">
						<c:set var="deleteIcon" value="delete" scope="request"/>
					</c:when>
					<c:otherwise>
						<c:set var="deleteIcon" value="arrow-r" scope="request"/>
					</c:otherwise>
				</c:choose>
				<c:if test="${fn:length(item[itemField.name]) gt 0}">
					<p>
						<strong>${fn:escapeXml(itemField.label)}</strong>
					</p> 
					<ul data-role="listview" data-split-icon="${fn:escapeXml(deleteIcon)}" data-split-theme="d">
						<% pageContext.setAttribute("itemField" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), pageContext.getAttribute("itemField", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
						<% pageContext.setAttribute("item" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), pageContext.getAttribute("childItem", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
						<c:set var="itemLevel" value="${itemLevel + 1}" scope="request"/>
						<c:forEach var="childItem" items="${item[itemField.name]}">
							<c:set var="item" value="${childItem}" scope="request"/>
							<li>
								<a href="/page/item/${itemField.itemName}/${itemField.itemName}View.jsp?id=${childItem.id}"> 
									<c:if test="${not empty item.photo}">
										<img src="${childItem.photo.uriPrefix}100${childItem.photo.uriSuffix}" />
									</c:if>
									<% pageContext.setAttribute("itemField" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), pageContext.getAttribute("itemField", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
									<% pageContext.setAttribute("item" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), pageContext.getAttribute("item", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
									<c:set var="itemLevel" value="${itemLevel + 1}" scope="request"/>
									
									<c:forEach var="childItemField" items="${childItem.fields}">
										<c:set var="itemField" value="${childItemField}" scope="request"/>
										
										<c:if test="${childItemField.inIndex}">
											<% pageContext.setAttribute("itemField" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), pageContext.getAttribute("itemField", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
											<% pageContext.setAttribute("item" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), pageContext.getAttribute("item", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
											<c:set var="editableClass" value="" scope="request"/>
											<c:set var="itemLevel" value="${itemLevel + 1}" scope="request"/>
											<jsp:include page="/fragment/ItemField.jsp"/>
											<c:set var="itemLevel" value="${itemLevel - 1}" scope="request"/>
											<% pageContext.setAttribute("itemField", pageContext.getAttribute("itemField" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
											<% pageContext.setAttribute("item", pageContext.getAttribute("item" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
										</c:if>
									</c:forEach>
									<c:set var="itemLevel" value="${itemLevel - 1}" scope="request"/>
									<% pageContext.setAttribute("itemField", pageContext.getAttribute("itemField" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
									<% pageContext.setAttribute("item", pageContext.getAttribute("item" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
								</a>
								<c:choose>
									<c:when test="${not empty user}">
										<a href="/fragment/ItemDelete.jsp?modelName=${fn:escapeXml(itemField.itemName)}&amp;modelDisplayName=${fn:escapeXml(itemField.itemDisplayName)}&amp;name=${fn:escapeXml(item.name)}&amp;id=${item.id}&amp;uri=${tate:encodeUri(tate:getRequestUriAndBeyond(pageContext.request))}" data-rel="dialog" data-transition="slideup">Delete ${fn:escapeXml(itemField.itemDisplayName)}</a>
									</c:when>
									<c:otherwise>
										<a href="/page/item/${itemField.itemName}/${itemField.itemName}View.jsp?id=${item.id}">View ${fn:escapeXml(itemField.itemDisplayName)}</a>
									</c:otherwise>
								</c:choose>
							</li>
						</c:forEach>
						<c:set var="itemLevel" value="${itemLevel - 1}" scope="request"/>
						<% pageContext.setAttribute("itemField", pageContext.getAttribute("itemField" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
						<% pageContext.setAttribute("item", pageContext.getAttribute("item" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
					</ul>
				</c:if>
			</c:when>
			<c:when test="${not empty itemField.itemName and not empty item[itemField.name]}">
				<p>
					<a  data-inline="true" data-icon="back" data-role="button" href="/page/item/${itemField.itemName}/${itemField.itemName}View.jsp?id=${fn:escapeXml(item[itemField.name].id)}">${fn:escapeXml(itemField.label)}${' '}${fn:escapeXml(item[itemField.name].name)}</a>
				</p>
			</c:when>
			<c:when test="${itemField.c.name == 'java.lang.String' and itemField.link}">
				<p>
					<a href="${fn:escapeXml(item[itemField.name])}">${fn:escapeXml(itemField.label)}</a>
				</p>
			</c:when>
			<c:when test="${not empty itemField.stars and (itemField.c.name == 'java.lang.Integer' or itemField.c.name == 'int')}">
				<c:if test="${not empty item[itemField.name] and item[itemField.name] gt 0}">
					<div class="stars-box">
						<c:forEach var="i" begin="1" end="${itemField.stars}" step="1" varStatus ="status">
							<c:choose>
								<c:when test="${item[itemField.name] == i}">
									<input name="${item.id}_${fn:escapeXml(itemField.name)}" type="radio" class="starReadOnly" value="${i}" checked="checked"/>
								</c:when>
								<c:otherwise>
									<input name="${item.id}_${fn:escapeXml(itemField.name)}" type="radio" class="starReadOnly" value="${i}"/>
								</c:otherwise>
							</c:choose>
						</c:forEach>
					</div>
				</c:if>
			</c:when>
			<c:otherwise>
				<c:choose>
					<c:when test="${not empty itemField.header}">
						<c:choose>
							<c:when test="${itemField.header == 1}">
								<h1>
									<c:choose>
										<c:when test="${not empty itemField.label}">
											<strong class="${itemAction}_${modelName}_${itemField.name}_label">${fn:escapeXml(itemField.label)}</strong>
										</c:when>
										<c:when test="${editableClass == 'editable'}">
											<strong class="${itemAction}_${modelName}_${itemField.name}_label">${fn:escapeXml(itemField.name)}: </strong>
										</c:when>
									</c:choose>
									<span class="${fn:escapeXml(editableClass)}${' '}${itemAction}_${modelName}_${itemField.name}" id="${modelName}_${item.id}_${itemField.name}">${fn:escapeXml(item[itemField.name])}</span>
								</h1>
							</c:when>
							<c:when test="${itemField.header == 2}">
								<h2>
									<c:choose>
										<c:when test="${not empty itemField.label}">
											<strong class="${itemAction}_${modelName}_${itemField.name}_label">${fn:escapeXml(itemField.label)}</strong>
										</c:when>
										<c:when test="${editableClass == 'editable'}">
											<strong class="${itemAction}_${modelName}_${itemField.name}_label">${fn:escapeXml(itemField.name)}: </strong>
										</c:when>
									</c:choose>
									<span class="${fn:escapeXml(editableClass)}${' '}${itemAction}_${modelName}_${itemField.name}" id="${modelName}_${item.id}_${itemField.name}">${fn:escapeXml(item[itemField.name])}</span>
								</h2>
							</c:when>
							<c:when test="${itemField.header == 3}">
								<h3>
									<c:choose>
										<c:when test="${not empty itemField.label}">
											<strong class="${itemAction}_${modelName}_${itemField.name}_label">${fn:escapeXml(itemField.label)}</strong>
										</c:when>
										<c:when test="${editableClass == 'editable'}">
											<strong class="${itemAction}_${modelName}_${itemField.name}_label">${fn:escapeXml(itemField.name)}: </strong>
										</c:when>
									</c:choose>
									<span class="${fn:escapeXml(editableClass)}${' '}${itemAction}_${modelName}_${itemField.name}" id="${modelName}_${item.id}_${itemField.name}">${fn:escapeXml(item[itemField.name])}</span>
								</h3>
							</c:when>
							<c:when test="${itemField.header == 4}">
								<h4>
									<c:choose>
										<c:when test="${not empty itemField.label}">
											<strong class="${itemAction}_${modelName}_${itemField.name}_label">${fn:escapeXml(itemField.label)}</strong>
										</c:when>
										<c:when test="${editableClass == 'editable'}">
											<strong class="${itemAction}_${modelName}_${itemField.name}_label">${fn:escapeXml(itemField.name)}: </strong>
										</c:when>
									</c:choose>
									<span class="${fn:escapeXml(editableClass)}${' '}${itemAction}_${modelName}_${itemField.name}" id="${modelName}_${item.id}_${itemField.name}">${fn:escapeXml(item[itemField.name])}</span>
								</h4>
							</c:when>
							<c:when test="${itemField.header == 5}">
								<h5>
									<c:choose>
										<c:when test="${not empty itemField.label}">
											<strong class="${itemAction}_${modelName}_${itemField.name}_label">${fn:escapeXml(itemField.label)}</strong>
										</c:when>
										<c:when test="${editableClass == 'editable'}">
											<strong class="${itemAction}_${modelName}_${itemField.name}_label">${fn:escapeXml(itemField.name)}: </strong>
										</c:when>
									</c:choose>
									<span class="${fn:escapeXml(editableClass)}${' '}${itemAction}_${modelName}_${itemField.name}" id="${modelName}_${item.id}_${itemField.name}">${fn:escapeXml(item[itemField.name])}</span>
								</h5>
							</c:when>
							<c:when test="${itemField.header == 6}">
								<h6>
									<c:choose>
										<c:when test="${not empty itemField.label}">
											<strong class="${itemAction}_${modelName}_${itemField.name}_label">${fn:escapeXml(itemField.label)}</strong>
										</c:when>
										<c:when test="${editableClass == 'editable'}">
											<strong class="${itemAction}_${modelName}_${itemField.name}_label">${fn:escapeXml(itemField.name)}: </strong>
										</c:when>
									</c:choose>
									<span class="${fn:escapeXml(editableClass)}${' '}${itemAction}_${modelName}_${itemField.name}" id="${modelName}_${item.id}_${itemField.name}">${fn:escapeXml(item[itemField.name])}</span>
								</h6>
							</c:when>
						</c:choose>
					</c:when>
					<c:when test="${not empty user or not empty item[itemField.name]}">
						<p>
							<c:choose>
								<c:when test="${not empty itemField.label}">
									<strong class="${itemAction}_${modelName}_${itemField.name}_label">${fn:escapeXml(itemField.label)}</strong>
								</c:when>
								<c:when test="${editableClass == 'editable'}">
									<strong class="${itemAction}_${modelName}_${itemField.name}_label">${fn:escapeXml(itemField.name)}: </strong>
								</c:when>
							</c:choose>
							<span class="${fn:escapeXml(editableClass)}${' '}${itemAction}_${modelName}_${itemField.name}" id="${modelName}_${item.id}_${itemField.name}"><%--
								--%><c:choose><%--
									--%><c:when test="${itemField.textarea}"><%--
										--%>${item[itemField.name]}<%--
									--%></c:when><%--
									--%><c:otherwise><%--
										--%>${fn:escapeXml(item[itemField.name])}<%--
									--%></c:otherwise><%--
								--%></c:choose>
							</span>
						</p>
					</c:when>
				</c:choose>
			</c:otherwise>
		</c:choose>
	</c:when>
	<c:when test="${itemField.class.name == 'org.computate.model.item.field.ItemParagraph'}">
		<p>${fn:escapeXml(itemField.text)}</p>
	</c:when>
</c:choose>
